package application;

import application.dao.DBService;
import application.dao.entity.User;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;

public class Manager_control_user_controller extends BaseController {

    @FXML
    public TextField nameText;
    @FXML
    public TextField realNameText;
    @FXML
    public Label showIdCard;
    @FXML
    public Label showMobile;
    @FXML
    public Label showAddress;
    @FXML
    public TextField settingRealName;
    @FXML
    public TextField settingName;
    @FXML
    private TextField inputInfo;

    @FXML
    private MenuItem menu_join_competitons;

    @FXML
    private Menu menu_main;

    @FXML
    private Menu onCheckMenu_main;

    @FXML
    private Button clickToReset;

    @FXML
    private Button clickToSearch;

    @FXML
    private TextField resetNum;

    @FXML
    private RadioButton searchIsMan;

    @FXML
    private RadioButton isman;

    @FXML
    private RadioButton searchIsWoman;

    @FXML
    private Menu menu_join;

    @FXML
    private Button approvalNotOK;

    @FXML
    private RadioButton iswoman;

    @FXML
    private Button isban;

    @FXML
    private MenuItem menu_join_users;

    @FXML
    private Button approvalOK;

    @FXML
    private Button notban;

    @FXML
    private Menu menu_informations;

    @FXML
    private Menu menu_scores;
    User settingUser;

    @FXML
    void onClickToSearch(ActionEvent event) {
        System.out.println("query");
        String name = nameText.getText();
        String realName = realNameText.getText();
        User user = DBService.queryUser(name, realName);
        if (null != user) {
            showUser(user);
        } else {
            BaseUtil.info("查无此人");
        }
    }

    private void showUser(User user) {
        String sex = user.getSex();
        if ("女".equals(sex)) {
            searchIsMan.setSelected(true);
        } else {
            searchIsMan.setSelected(true);
        }
        showIdCard.setText(user.getIdCard());
        showMobile.setText(user.getTelNumber());
        showAddress.setText(user.getHomeAddress());
    }

    @FXML
    void onClickSearchIsMan(ActionEvent event) {

    }

    @FXML
    void onClickSearchIsWoman(ActionEvent event) {

    }

    @FXML
    void onClickIsMan(ActionEvent event) {

    }

    @FXML
    void onClickIsWoman(ActionEvent event) {

    }

    @FXML
    void onButtonApprovalOK(ActionEvent event) {
        System.out.println("审批");
        auditUser(1);
    }

    @FXML
    void onButtonApprovalNotOK(ActionEvent event) {
        System.out.println("不审批");
        auditUser(0);
    }

    private void auditUser(int code) {
        Object auditUser = map.get("auditUser");
        if (null != auditUser) {
            User user = (User) auditUser;
            Boolean aBoolean = DBService.auditUser(user.getID(), user.getActApproval());
            BaseUtil.info("result:" + aBoolean);
        } else {
            BaseUtil.info("没有审批人员");
        }
    }


    @FXML
    void onClickIsBan(ActionEvent event) {
        System.out.println("forbid");
        updateUser(0);
    }

    @FXML
    void onClickNotBan(ActionEvent event) {
        System.out.println("jie jin");
        updateUser(1);
    }

    private void updateUser(int status) {
        User user = DBService.queryUser(settingName.getText(), settingRealName.getText());
        if (null != user) {
            this.settingUser = user;
            DBService.forbidUser(user.getID(), status);
        } else {
            BaseUtil.info("查无此人");
        }
    }


    @FXML
    void onClickToReset(ActionEvent event) {
        System.out.println("重置");
        if (null != this.settingUser) {
            String text = resetNum.getText();
            settingUser.setPassWord(text);
            Boolean aBoolean = DBService.modifyPwd(settingUser);
            BaseUtil.info("修改结果:" + aBoolean);
        }
    }

    @FXML
    void onCheckMenu_main(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_person(ActionEvent event) {

    }

    @FXML
    void c(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_join_users(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_join_competitons(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_scores(ActionEvent event) {

    }

    @FXML
    void onCheckMenu_informations(ActionEvent event) {

    }


    @Override
    public void init() {

    }
}